package com.jessehu.xutilstest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.jessehu.xutilstest.utils.DebugLog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class HttpTestActivity extends AppCompatActivity {

    private HttpTestActivity TAG;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_test);
        TAG = this;
        tvResult = (TextView) findViewById(R.id.tv_http_result);
        getData();
    }

    //获取网络数据
    private void getData(){
        //慕课网的链接不支持post
        String dataUrl = "http://www.imooc.com/api/teacher?type=4&num=30";
        RequestParams params = new RequestParams(dataUrl);

        String dataUrl2 = "http://www.imooc.com/api/teacher";
        RequestParams params2 = new RequestParams(dataUrl2);
        params2.addParameter("type","4");
        params2.addParameter("num","30");

        requestByGet(params);
        requestByGet(params2);

        String dataUrl3 = "http://api.k780.com:88/";
        RequestParams params3 = new RequestParams(dataUrl3);
        params3.addParameter("app","idcard.get");
        params3.addParameter("appkey","10003");
        params3.addParameter("sign","b59bc3ef6191eb9f747dd4e83c99f2a4");
        params3.addParameter("format","json");
        params3.addParameter("idcard","110101199001011114");
        requestByPost(params3);
    }

    //post请求
    private void requestByPost(RequestParams params) {
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                DebugLog.i("post",result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //get请求
    private void requestByGet(RequestParams params) {
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                DebugLog.i("get",result);
                tvResult.setText(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
